---@class component_mod_pono_diving_movement_block
---@field inst ent
---@field blocked boolean 是否阻止移动
local mod_pono_diving_movement_block = Class(function(self, inst)
    self.inst = inst
    self.blocked = false
    
    -- 延迟Hook,确保locomotor已经初始化
    inst:DoTaskInTime(0, function()
        if inst.components.locomotor then
            self:HookLocomotor()
        end
        self:HookBufferedAction()
    end)
end)

-- Hook locomotor 的各个移动函数
function mod_pono_diving_movement_block:HookLocomotor()
    local inst = self.inst
    local locomotor = inst.components.locomotor
    
    if not locomotor then
        return
    end
    
    -- 保存原始函数引用到inst上
    if not inst._pono_locomotor_hooked then
        inst._pono_locomotor_hooked = true
        
        -- 保存原始函数
        inst._pono_orig_RunForward = locomotor.RunForward
        inst._pono_orig_RunInDirection = locomotor.RunInDirection
        inst._pono_orig_WalkForward = locomotor.WalkForward
        inst._pono_orig_WalkInDirection = locomotor.WalkInDirection
        inst._pono_orig_GoToPoint = locomotor.GoToPoint
        inst._pono_orig_PushAction = locomotor.PushAction
        
        -- Hook RunForward
        locomotor.RunForward = function(loco_self)
            if inst:HasTag("pono_in_pond") then
                return  -- 在池塘中，直接返回不移动
            end
            return inst._pono_orig_RunForward(loco_self)
        end
        
        -- Hook RunInDirection
        locomotor.RunInDirection = function(loco_self, direction, throttle)
            if inst:HasTag("pono_in_pond") then
                return  -- 在池塘中，直接返回不移动
            end
            return inst._pono_orig_RunInDirection(loco_self, direction, throttle)
        end
        
        -- Hook WalkForward
        locomotor.WalkForward = function(loco_self)
            if inst:HasTag("pono_in_pond") then
                return  -- 在池塘中，直接返回不移动
            end
            return inst._pono_orig_WalkForward(loco_self)
        end
        
        -- Hook WalkInDirection
        locomotor.WalkInDirection = function(loco_self, direction, throttle)
            if inst:HasTag("pono_in_pond") then
                return  -- 在池塘中，直接返回不移动
            end
            return inst._pono_orig_WalkInDirection(loco_self, direction, throttle)
        end
        
        -- Hook GoToPoint (鼠标点击地面移动)
        locomotor.GoToPoint = function(loco_self, pt, cb, tryhard, ignore_physics)
            if inst:HasTag("pono_in_pond") then
                print("[PonoDiving] Blocked GoToPoint during diving")
                return  -- 在池塘中，阻止移动
            end
            return inst._pono_orig_GoToPoint(loco_self, pt, cb, tryhard, ignore_physics)
        end
        
        -- Hook PushAction (点击物品等带移动的动作)
        if locomotor.PushAction then
            locomotor.PushAction = function(loco_self, buffered_action, run)
                if inst:HasTag("pono_in_pond") then
                    -- 检查是否是点击池塘的动作(跃出)
                    if buffered_action and buffered_action.action then
                        local action_id = buffered_action.action.id or buffered_action.action
                        -- 允许 PONO_DIVE_OUT 动作
                        if action_id == "PONO_DIVE_OUT" then
                            print("[PonoDiving] Allowing PONO_DIVE_OUT action in PushAction")
                            return inst._pono_orig_PushAction(loco_self, buffered_action, run)
                        end
                    end
                    print("[PonoDiving] Blocked PushAction during diving")
                    return  -- 在池塘中，阻止其他移动
                end
                return inst._pono_orig_PushAction(loco_self, buffered_action, run)
            end
        end
        
        print("[PonoDiving] Locomotor movement functions hooked by component")
    end
end

-- Hook 角色的 PushBufferedAction (点击物品等动作)
function mod_pono_diving_movement_block:HookBufferedAction()
    local inst = self.inst
    
    if not inst._pono_buffered_action_hooked then
        inst._pono_buffered_action_hooked = true
        
        -- 保存原始函数
        inst._pono_orig_PushBufferedAction = inst.PushBufferedAction
        
        -- Hook PushBufferedAction
        inst.PushBufferedAction = function(inst_self, buffered_action)
            if inst:HasTag("pono_in_pond") then
                -- 检查是否是跃出池塘的动作
                if buffered_action and buffered_action.action then
                    local action_id = buffered_action.action.id or buffered_action.action
                    if action_id == "PONO_DIVE_OUT" then
                        print("[PonoDiving] Allowing PONO_DIVE_OUT in PushBufferedAction")
                        return inst._pono_orig_PushBufferedAction(inst_self, buffered_action)
                    end
                end
                print("[PonoDiving] Blocked buffered action during diving:", buffered_action and buffered_action.action and buffered_action.action.id or "unknown")
                return false  -- 阻止其他动作
            end
            return inst._pono_orig_PushBufferedAction(inst_self, buffered_action)
        end
        
        print("[PonoDiving] PushBufferedAction hooked by component")
    end
end

-- 启用移动阻止
function mod_pono_diving_movement_block:Enable()
    self.blocked = true
end

-- 禁用移动阻止
function mod_pono_diving_movement_block:Disable()
    self.blocked = false
end

return mod_pono_diving_movement_block
